﻿using Microsoft.AspNetCore.Mvc;
using System.Text.Json;

namespace Apollo.ApiTest.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class TestApolloClientController : ControllerBase
    {
        private readonly IConfiguration _configuration;

        public TestApolloClientController(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        /// <summary>
        /// GetConfig
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetApolloConfig")]
        public List<string> GetApolloConfig()
        {
            var result = new List<string>();
            var config1 = _configuration["Conn"]; //value如果是字符串，直接读取
            var config2 = _configuration["key2"]; //如果值为JSON格式，无法通过_configuration[key]读取
            var config3 = _configuration["testkey01"];
            var config4 = _configuration["key1:kk1"];
            var config5 = _configuration.GetSection("key2").Get<TestUserInfo>();

            var config6 = _configuration.GetSection("key3").Get<List<TestUserInfo>>();
            var config7 = _configuration.GetSection("test02:key3").Get<List<TestUserInfo>>();

            result.Add(config1);
            result.Add(config2);
            result.Add(config3);
            result.Add(config4);

            result.Add(JsonSerializer.Serialize(config5));
            return result;
        }


    }


    public class TestUserInfo
    {
        public string Name { get; set; }

        public int Age { get; set; }
    }

}
